﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using System.Text;

namespace Vms.Web.Lib
{
    /// <summary>
    /// Read and Write data with ini file
    /// </summary>
    public class IniFile
    {
        [DllImport("kernel32")]
        private static extern long 
            WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int
            GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// Write data to ini file.
        /// </summary>
        /// <param name="section">section</param>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        /// <param name="filePath">file path</param>
        public static void Write(string section, string key, string value, string filePath)
        {
            WritePrivateProfileString(section, key, value, filePath);
        }

        /// <summary>
        /// Read value from ini data.
        /// </summary>
        /// <param name="section">section</param>
        /// <param name="key">key</param>
        /// <param name="filePath">file path</param>
        /// <returns>value</returns>
        public static string Read(string section, string key, string filePath)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, filePath);
            return temp.ToString();
        }
    }
}